/**
 * copyright (c) 2017-2023 厦门风领科技有限公司
 * https://www.fengling.cn/
 * 
 * h5小游戏适配
 * zengbinsi
 * 2020-08-15 15:37:25
 * 2023-10-23 15:32:11
 */
window.gg = window.gg || {};

if (cc.sys.isBrowser) {
    console.log('[TS-API] ===>>>', '适配H5平台');

    gg.setClipboardData = (object) => {
        return new Promise((resolve, reject) => {
            try {
                //网页
                const save = function (e) {
                    e.clipboardData.setData("text/plain", object.data);
                    e.preventDefault();

                    const res = { data: object.data };
                    object.success && object.success(res);
                    object.complete && object.complete();
                    resolve(res);
                }.bind(this);

                document.addEventListener("copy", save);
                document.execCommand("copy");
                document.removeEventListener("copy", save);
            } catch (error) {
                object.fail && object.fail(error);
                object.complete && object.complete();
                reject(error);
            }
        });
    };

    gg.openBrowserUrl = (url, isNewWindow) => {
        gg.isEmpty(isNewWindow) && (isNewWindow = true);

        if (isNewWindow) {
            window.open(url);
        } else {
            window.location.href = url;
        }
    };

    // 封装的弹窗
    var toastTimer;
    gg.showToast = (data) => {
        return new Promise((resolve, reject) => {
            try {
                clearTimeout(toastTimer);

                var ToastBox = document.getElementsByClassName('toastDiv-box')[0];
                if (ToastBox) document.body.removeChild(ToastBox);//防止重复弹出
                duration = isNaN(data.duration) ? 1500 : data.duration;
                var m = document.createElement('div');
                m.className = 'toastDiv-box';
                m.innerHTML = data.title;
                m.style.cssText = `
                    width: 70%;
                    min-width: 50px;
                    opacity: 0.7;
                    height: auto;
                    color: rgb(255, 255, 255);
                    text-align: center;
                    padding: 10px;
                    border-radius: 5px;
                    position: fixed;
                    top: 70%;
                    left: 13%;
                    z-index: 999999;
                    background: rgb(0, 0, 0);
                    font-size: 14px;
                    overflow: hidden;
                `;
                document.body.appendChild(m);
                toastTimer = setTimeout(function () {
                    var d = 0.5;
                    m.style.webkitTransition = '-webkit-transform ' + d + 's ease-in, opacity ' + d + 's ease-in';
                    m.style.opacity = '0';
                    setTimeout(function () { document.body.removeChild(m); }, d * 1000);
                }, duration);
                data.success && data.success();
                resolve();
            } catch (error) {
                data.fail && data.fail(error);
                reject(error);
            }
            data.complete && data.complete();
        });
    };

    gg.showModal = (data) => {
        return new Promise((resolve, reject) => {
            try {
                gg.isEmpty(data.showCancel) && (data.showCancel = true);
                let res;
                if (data.editable) {
                    res = prompt(data.placeholderText || '');
                    data.success && data.success({ confirm: true, cancel: false, content: res });
                } else if (data.showCancel) {
                    res = confirm(data.content);
                    data.success && data.success(res ? { confirm: true, cancel: false } : { confirm: false, cancel: true });
                } else {
                    alert(data.content);
                    data.success && data.success(res ? { confirm: true, cancel: false } : { confirm: false, cancel: true });
                }
                resolve(res);
            } catch (error) {
                data.fail && data.fail(error);
                reject(error);
            }
            data.complete && data.complete();
        });
    };

    gg.login = (data) => {
        return new Promise((resolve, reject) => {
            data && data.success && data.success({ code: Date.now(), data: {} });
        });
    };

    // 模拟广告
    gg.createRewardedVideoAd = (data) => {
        gg.log('该平台暂未实现gg.createRewardedVideoAd()方法。');
        return {
            load: () => { return Promise.resolve(); },
            show: () => { return Promise.resolve(); },
            destroy: () => { },

            onLoad: () => { },
            offLoad: () => { },
            onClose: () => { },
            offClose: () => { },
            onError: () => { },
            offError: () => { },
        };
    };
}